//
//  MapData.m
//  DefenseSummoner
//
//  Created by vupham on 5/28/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "MapDataParser.h"
@implementation MapDataParser

@synthesize mapData;

-(id)initMapData{
    self=[super init];
    mapData=[[MapData alloc]initMapData];
    return self;
}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName 
  namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName 
	attributes:(NSDictionary *)attributeDict 
{
    if ([elementName isEqualToString:@"Wave"]) {
        wave = [[NSMutableArray alloc]init];
    }
    else if ([elementName isEqualToString:@"Enemy"]) {
        enemyInformation = [[NSMutableDictionary alloc]init];
        [enemyInformation setObject:attributeDict forKey:@"attribute"];
    }
}


// NSXMLParser delegate method

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string { 
	
	string = [string stringByReplacingOccurrencesOfString:@"\t" withString:@""];
	string = [string stringByReplacingOccurrencesOfString:@"\n" withString:@""];	
	if(!currentElementValue) 
		currentElementValue = [[NSMutableString alloc] initWithString:string];
	else
		[currentElementValue appendString:string];
    //NSLog(@"Processing Value: %@", currentElementValue);
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName 
  namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName 
{
	
    currentElementValue=[currentElementValue stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    if ([elementName isEqualToString:@"TileMap"]){
        mapData.mapBackground = currentElementValue;
        //NSLog(@"Map name=%@",mapSprite);
        //NSLog(@"current map image: %@", currentElementValue);
    }
    else if ([elementName isEqualToString:@"Money"]){
        mapData.startingMoney = [currentElementValue intValue];
    }
    else if ([elementName isEqualToString:@"NumberOfWaypoints"]){
        mapData.numberOfWaypoints = [currentElementValue intValue];
    }
    else if ([elementName isEqualToString:@"Enemy"]) {
        int quantity=[[[enemyInformation objectForKey:@"attribute"] valueForKey:@"quantity"] intValue];
        //        NSLog(@"Testing %@",[[enemyInformation objectForKey:@"attribute"] valueForKey:@"quantity"]);
        for (int i=0;i<quantity;i++){
            EnemyInfo *waveInfo=[[EnemyInfo alloc]init];
            waveInfo.enemyType=[[enemyInformation objectForKey:@"attribute"] valueForKey:@"type"];
            waveInfo.waypointID=[[[enemyInformation objectForKey:@"attribute"] valueForKey:@"usingWayPoint"] intValue];
            [wave addObject:waveInfo];
            //            NSLog(@"Enemy Type: %@",waveInfo.enemyType);
            //            NSLog(@"Waypoint ID: %d",waveInfo.waypointID);
        }
    }
    else if ([elementName isEqualToString:@"Wave"]) {
        [mapData.waves addObject:wave];
        //        NSLog(@"Number Of Waves");
    }
	currentElementValue = nil;
}

@end
